package com.gh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gh.model.hosp.Department;
import com.gh.model.hosp.Hospital;
import com.gh.repository.DepartmentRepository;
import com.gh.service.DepartmentService;
import com.gh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/6/9 20:09
 * @Version 1.0
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        String str = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(str, Department.class);
        Department departmentExist=departmentRepository.getDepartmentByhoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(departmentExist!=null){
            //更新
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            //添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> getByHoscode(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象,设置当前页和每页显示数
        Pageable pageable = PageRequest.of(page-1,limit);
        Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        //创建example对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,exampleMatcher);
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByhoscodeAndDepcode(hoscode, depcode);
        if(department==null){
            return;
        }
        String id = department.getId();
        departmentRepository.deleteById(id);
    }

    @Override
    public String getDepartName(String hoscode, String depcode) {

        Department department = departmentRepository.getDepartmentByhoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByhoscodeAndDepcode(hoscode, depcode);
    }
}
